Skip to main content
OpenCLIP provides flexible model loading with support for pretrained weights, custom configurations, and multiple storage backends.

Basic Model Loading

create_model()

The core function for creating CLIP models with flexible configuration options.
str
required
Model architecture name (e.g., ‘ViT-B-32’, ‘RN50’) or schema-prefixed path:
  • Built-in: 'ViT-B-32'
  • HuggingFace Hub: 'hf-hub:org/repo'
  • Local directory: 'local-dir:/path/to/model'
str
Pretrained weights source. Can be:
  • Tag name (e.g., ‘openai’, ‘laion2b_s34b_b79k’)
  • Local file path (e.g., ‘/path/to/weights.pt’)
  • Ignored if model_name uses schema prefix
str | torch.device
default:"cpu"
Device to load model on (‘cpu’, ‘cuda’, etc.)
str
default:"fp32"
Model precision: ‘fp32’, ‘fp16’, ‘bf16’, ‘pure_fp16’, ‘pure_bf16’
bool
default:"False"
Whether to JIT compile the model
int | Tuple[int, int]
Override default image size for the model
str
Directory for caching downloaded weights

Loading Schemas

HuggingFace Hub

Load models directly from HuggingFace Hub using the hf-hub: schema:
The function automatically:
  • Downloads open_clip_config.json from the repo
  • Looks for weights files (.safetensors, .bin, .pth)
  • Merges preprocessing configuration

Local Directory

Load from a local directory containing model config and weights:
Local directory must contain:
  • open_clip_config.json with model configuration
  • Weight file (searched in order): open_clip_model.safetensors, pytorch_model.bin, model.pth, etc.

Local File Path

Load weights from a specific file:

Advanced Loading Options

Tower-Specific Weights

Load separate weights for image and text towers:
bool
default:"False"
Load default pretrained weights for image tower (timm models)
bool
default:"True"
Load default pretrained weights for text tower (HuggingFace models)
str
Path to custom image tower weights (loaded after full model)
str
Path to custom text tower weights (loaded after full model)

Custom Model Configuration

Override model architecture parameters:

create_model_and_transforms()

Convenience function that returns model with preprocessing transforms:
Returns a tuple of (model, train_transform, val_transform). The transforms handle:
  • Image resizing and cropping
  • Normalization with correct mean/std
  • Data augmentation (training only)
Always use model.eval() before inference. Models are in training mode by default, which affects layers like BatchNorm.

create_model_from_pretrained()

Strictly requires pretrained weights (raises error if weights can’t be loaded):
bool
default:"True"
Whether to return preprocessing transform. If False, returns only model.
This is the recommended function for inference use cases where pretrained weights are essential.

Listing Available Models

Weight Loading Options

bool
default:"True"
Whether to load the resolved pretrained weights. Set to False for random initialization.
bool
default:"False"
Raise error if pretrained weights cannot be loaded
bool
default:"True"
Use weights_only=True for torch.load (safer, prevents arbitrary code execution)

Complete Example